package com.bootdo.common.qiniuutils;
import java.io.FileNotFoundException;
import java.io.InputStream;




public class FileUploadUtils {
	public static String accessKey = "d6C82P__TP1ADJnKqDPM7HqkuCa3Fa2jeTSk6JVH";
	public static String secretKey = "xM5zYmM0ftZrnC0Nbkanv98HQgsTWfFadzbSUHqb";
	public static String fileDefalUrl = "http://pifnn6fw6.bkt.clouddn.com/";
	public static String bucket = "netdisk";
	
	
	
	/*public  static  String fileUp(InputStream input,String fileName) {
		//构造一个带指定Zone对象的配置类
		Configuration cfg = new Configuration(Zone.zone0());
		//...其他参数参考类注释
		UploadManager uploadManager = new UploadManager(cfg);
		//默认不指定key的情况下，以文件内容的hash值作为文件名
		Auth auth = Auth.create(accessKey, secretKey);
		String upToken = auth.uploadToken(bucket);
		try {
		    Response response = uploadManager.put(input,fileName,upToken,null, null);
		    //解析上传成功的结果
		    DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
			System.out.println("putret ->?" + response.bodyString());
		    return fileDefalUrl+putRet.key;			
		} catch (QiniuException ex) {
		    Response r = ex.response;
		    System.err.println(r.toString());
		    try {
		        System.err.println(r.bodyString());
		    } catch (QiniuException ex2) {
		        //ignore
		    }
		}
		return null;
	}
	
	
	
	*//**
	 * 如果抛出异常说明删除失败
	 * @param fileName
	 * @return
	 * @throws QiniuException
	 *//*
	public static String fileUp(String filePath,String fileName) throws QiniuException {
		//构造一个带指定Zone对象的配置类
		Configuration cfg = new Configuration(Zone.zone0());
		//...其他参数参考类注释
		UploadManager uploadManager = new UploadManager(cfg);
		//...生成上传凭证，然后准备上传
		//默认不指定key的情况下，以文件内容的hash值作为文件名
		String key = null;
		Auth auth = Auth.create(accessKey, secretKey);
		String upToken = auth.uploadToken(bucket);
	
		Response response = uploadManager.put(fileName, key, upToken);
		    //解析上传成功的结果
		DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
		System.out.println("putret ->?" + response.bodyString());
		 return fileDefalUrl+putRet.key;			
				
	}
	
	*//**
	 * 删除文件  填入文件名称 如果抛出异常说明删除失败
	 *//*
	public static void fileDelete(String fileName) throws QiniuException {
		//构造一个带指定Zone对象的配置类
		Configuration cfg = new Configuration(Zone.zone0());
		Auth auth = Auth.create(accessKey, secretKey);
		BucketManager bucketManager = new BucketManager(auth, cfg);
		Response res =  bucketManager.delete(bucket, fileName);	
	}*/
	
	
	public static void main(String[] args) throws FileNotFoundException {
		
		
		//System.err.println(fileUp(new FileInputStream(new File("c:/zckj.png"))));
		
		//System.err.println(fileUp("c:/zckj.png"));
		/*try {
			fileDelete("Fv-BbF2q5VKJhJ8t3BIMiYL7q2hs");
		} catch (QiniuException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
	}
}
